import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class GuessColumn extends JPanel {

	// sets up a column with four pegs in it
	final int PEGS_PER_COLUMN = 4;
	Peg pegs[];
	MouseHandler mouseHandler;
	Sound click = new Sound("src/sounds/click.wav");

	public GuessColumn() {

		// sets the array of pegs to have 4 guesses
		pegs = new Peg[PEGS_PER_COLUMN];

		for (int i = 0; i < PEGS_PER_COLUMN; i++) {
			pegs[i] = new Peg();
		}

		// declare the mouse handler
		mouseHandler = new MouseHandler();
		setLayout(new GridLayout(4, 1));
		setPreferredSize(new Dimension(34, 140));

		// add the pegs to the GuessColumn panel
		for (int i = 0; i < PEGS_PER_COLUMN; i++) {
			this.add(pegs[i]);
		}
	}

	// sets whether or not the current column will be clickable
	// only the current turn can be clickable
	public void Clickable(boolean value) {

		if (value == true) {
			for (int i = 0; i < PEGS_PER_COLUMN; i++) {
				pegs[i].addMouseListener(mouseHandler);
			}
		} else {
			for (int i = 0; i < PEGS_PER_COLUMN; i++) {
				pegs[i].removeMouseListener(mouseHandler);
			}
		}
	}

	// get the values of the pegs that are the in column
	// gets the state
	public int[] getValues() {
		int[] retValues = new int[PEGS_PER_COLUMN];
		for (int i = 0; i < PEGS_PER_COLUMN; i++) {
			retValues[i] = pegs[i].getState();
		}
		return retValues;
	}

	// set the state of each peg
	public void setPegValues(int[] values) {
		for (int i = 0; i < PEGS_PER_COLUMN; i++) {
			pegs[i].setState(values[i]);
		}
	}

	// clear the column
	public void clear() {
		for (int i = 0; i < PEGS_PER_COLUMN; i++) {
			pegs[i].setState(0);
		}
	}

	// when clicked, the panel cycles through the colors 
	class MouseHandler implements MouseListener {
		public void mousePressed(MouseEvent e) {
			for (int i = 0; i < PEGS_PER_COLUMN; i++) {
				if (e.getSource().equals(pegs[i]))
					pegs[i].switchColor();
			}
			click.run();
		}

		public void mouseReleased(MouseEvent e) {
		}

		public void mouseEntered(MouseEvent e) {
		}

		public void mouseExited(MouseEvent e) {
		}

		public void mouseClicked(MouseEvent e) {
		}
	}
}
